calendar_data_cycleType={
	NONE:"none",
	DAY :"day",
	WEEK:"week",
	MONTH:"month"
};
function getServerDate(){
	var BJTIME=(typeof(_bjtime)!="undefined" && _bjtime!=null) ? _bjtime.toString() : null;
	if(BJTIME!=null && BJTIME.length<=10) BJTIME+="000";
	return BJTIME!=null ? new Date(parseInt(BJTIME)) : new Date();
}
(function($) { // hide the namespace
if(!Array.prototype.unique){
	Array.prototype.unique = function () {
			var r = new Array();
			o:for(var i = 0, n = this.length; i < n; i++)
			{
				for(var x = 0, y = r.length; x < y; x++)
				{
					if(r[x]==this[i])
					{
						continue o;
					}
				}
				r[r.length] = this[i];
			}
			return r;
		};
	}
var calendarPickerBaseMethods={
	
	debug:true,
	/* Debug logging (if enabled). */
	log: function () {
		if (this.debug)
			console.log.apply('', arguments);
	},
	unique :function(a){
		var r = new Array();
		o:for(var i = 0, n = a.length; i < n; i++) {
			for(var x = i + 1 ; x < n; x++){
				if(a[x]==a[i]) continue o;
			}
				r[r.length] = a[i];
		 	}
			return r;
		},
	dataTemp:[],
	dataItemTemplate:{
			name :'',                                     //活动名称
			kind :'',                                     //种类
			text :'',       //文字说明
			url  :'',       //链接

			type :0,        //循环类型
			week :null,    //第几周
			day  :null,    //星期几
			date :null,    //一个月中的日期值 1～31
			fullDate:null, //完整的日期值
/*			last :0,
			priority:0,
			flash   :0,*/
			weekEntity:[],
			dayEntity:[],
			dateEntity:[]
		},

	baseTime:null,
	/** get date(ms) from server or specific time */
	getServerDate:function(_time){
			var BJTIME=_time || (typeof(_bjtime)!="undefined") ? _bjtime : null;
			if(BJTIME!=null && BJTIME.toString().length<=10) BJTIME+="000";
			return this.baseTime=BJTIME!=null ? new Date(parseInt(BJTIME)) : new Date();
		},

	setBaseTime:function(time){
			this.baseTime=getServerDate(time);
			return this;
		},

	/** find the object list with n[k]==value */
	objectMatch:function( obj ,key , value ){
			return jQuery.grep(obj,function(n,i){
				if(n[key]==null||n[key]=="") return false;
				for(var j=0;j< n[key].length;j++){
					if(value==n[key][j]) return true;
				}
				return false;
			});
		},
	/** find the object list with n[k]==value */
	fullDateMatch:function( obj ,key , value ){
			return jQuery.grep(obj,function(n,i){
				if(n[key]==null) return false;
				for(var j=0;j< n[key].length;j++){
					var _d=new Date(value),_dt=new Date(n[key][j]);
					if(_d.getDate()==_dt.getDate() && _d.getMonth()==_dt.getMonth() && _d.getYear()==_d.getYear() ) return true;
				}
				return false;
			});
		},

	/** find the data match the type */
	searchByCycleType:function( obj , type ){
			return jQuery.grep(obj,function(n,i){
				return n.type==type;
			});
		},

	/** find the object list with n[k]=value.toString() */
	searchCycle:function(obj , key , value){
			return jQuery.grep(obj,function(n,i){
				if(n[key]==null) return false;
				return jQuery.inArray(""+value, n[key] )!=-1;
			});
		},

	/**
	* function(obj[,str1][,str2][...])
	* return array
	* 24=24日，3/24=3月24，3/24/2009=2009年3月24日
	* 可以用- 来分隔，但一个日期中必须统一
	* dd, mm/dd, mm-dd, mm/dd/yyyy, mm-dd-yyyy
	*/
	dateStringPaser:function(){
		var args=Array.prototype.slice.call(arguments,0),
			today    				 = this.getServerDate(),
			targetDayArray   = [],
			i=0;
		for(;i<args.length;i++){
			var str=args[i].toString(),//sep=str.indexOf("\/")!=-1? "\/" : "-";
			dateSplit = str.split(/\/|-/g),targetDay=null;
			switch(dateSplit.length){
				case 1 :
					targetDay=new Date(today.getFullYear(),today.getMonth(),dateSplit[0]);
					break;
				case 2 :
					targetDay=new Date(today.getFullYear(),parseInt(dateSplit[0])-1,dateSplit[1]);
					break;
				case 3 :
					targetDay=new Date(dateSplit[2],parseInt(dateSplit[0])-1,dateSplit[1]);	
					break;
				default :
					targetDay=today;
			}
			targetDayArray=targetDayArray.concat([targetDay.getTime()]);	
		}
		return targetDayArray;
	},

	/**
	* function(dateVal) dateVal is ms from 1970
	* return array
	* 把一个月以周为长度分区段
	*/
	serializeDateByWeek:function(dateVal){
		var tempDate=new Date(dateVal);
		tempDate.setDate(1);
		var firstDay=tempDate.getDay();
		tempDate.setMonth(tempDate.getMonth()+1);
		tempDate.setDate(0);
		var maxDate=tempDate.getDate();

		var _serialDates=[],_dayCounter=firstDay,_weekArryTemp=[];
		for(var i=1;i<=maxDate;i++){
			_weekArryTemp=_weekArryTemp.concat([i]);
			if(_dayCounter==0 || i==maxDate){
				_serialDates=_serialDates.concat([_weekArryTemp]);
				_weekArryTemp=[];
			}
			//_dayCounter++;
			if(++_dayCounter==7) _dayCounter=0;
		}
		return _serialDates;
	},

	/**
	* return -1 if not found
	* 返回当前日期处于当月的第几周
	*/
	indexWeekOfMonth:function(dateVal){
		var sdDates=this.serializeDateByWeek(dateVal);
		var tempDate=new Date(dateVal),theDate=tempDate.getDate();
		for(var i=0;i<sdDates.length;i++){
			for(var j=0;j<sdDates[i].length;j++){
				if(theDate==sdDates[i][j]) return i;
			}
		}
		return -1;
	},

	/** parse '1,2,3' to [1,2,3] */
	parseStringToArray:function(str,sep){
		var _arr=  str.split(sep || ",");
		return _arr;
	},

	/**
	* parse "1,2,3,4,odd" to [1,2,3,4,5,7,9,...] even=[2,4,6,8,...],all=[1,2,3,4,5,6,7,...]
	* 把字符描述全部专为具体的实体数值
	*/
	parseStringToEntity:function(str,max,start,sep){
		var _arr=  str.split(sep || ","),_data=[],i=0,_max=max || 7;
		if(jQuery.inArray("odd",_arr)!=-1) for(i=1;i<=_max;i+=2) _data=_data.concat([i]);
		else if(jQuery.inArray("even",_arr)!=-1) for(i=2;i<=_max;i+=2) _data=_data.concat([i]);
		else if(jQuery.inArray("all",_arr)!=-1) for(i=1;i<=_max;i++) _data=_data.concat([i]);
		_arr=_arr.concat(_data);
		_arr=jQuery.grep( _arr, function(n,i){
			if(/\d+/.test(n)) return true;
			else return false;
		});
		_arr=_arr.unique();
		jQuery.each(_arr,function(i){_arr[i]=parseInt(this);});
		return _arr.sort(function(a,b){return a-b;});
	},

	/**
	* function(dateVal1[,dateVal2][,dateVal3][,...]) 
	* return events match the date
	* 可以用- 来分隔，但一个日期中必须统一
	*/
	_searchByDate:function(){
			var args         = Array.prototype.slice.call(arguments,0),
				_data          = args[0],
				_dateArray     = args.slice(1),
				_matchedArray  = [],
				today          = this.getServerDate(),
				targetDayArray = _dateArray;//dateStringPaser.apply(null,_dateArray);
			//this.log(args);
			_matchedArray=_matchedArray.concat(this.searchByCycleType(_data,"day"));//每天
			for(var i=0,n=targetDayArray.length; i<n ;i++){
				var thisElem=targetDayArray[i];
						_matchedArray=_matchedArray.concat(this.fullDateMatch(_data,"fullDate",thisElem));//具体到的日期
						
				var thisDateObj = new Date(parseInt(thisElem)),
						weekIndex   = this.indexWeekOfMonth(thisElem);

				var _monthCycle=jQuery.grep(_data,function(n,i){
						if(n.type!="month") return false;
						if(
							 (n.week!=null && ( jQuery.inArray((weekIndex+1), n.weekEntity )!=-1 ) && ( jQuery.inArray(thisDateObj.getDay(), n.dayEntity )!=-1 ))
								||
							 (n.date!=null && ( jQuery.inArray(thisDateObj.getDate(), n.dateEntity )!=-1 ))
							 ) return true;
						return false;
					});
				var _weekCycle=jQuery.grep(_data,function(n,i){
					if(n.type!=calendar_data_cycleType.WEEK) return false;
					if(n.day!=null && (jQuery.inArray(thisDateObj.getDay(), n.dayEntity )!=-1)) return true;
					return false;
				});
				_matchedArray=_matchedArray.concat(_monthCycle,_weekCycle);
			}
			return _matchedArray.unique();
		},

	searchByDate:function(){
		var args = Array.prototype.slice.call(arguments,0)
		return this._searchByDate.apply(this,[this.dataTemp].concat(args));
	},

	/** parse the data */
	parseData:function(data){
			for(var i=0,n=data.length ; i<n ; i++){
				var dataItem=data[i];
				var dataItemTemp={};
				jQuery.extend(dataItemTemp,this.dataItemTemplate);
				jQuery.extend(dataItemTemp,dataItem);
				dataItemTemp._week=dataItemTemp.week;
				dataItemTemp._day=dataItemTemp.day;
				dataItemTemp._date=dataItemTemp.date;
				dataItemTemp._fullDate=dataItemTemp.fullDate;
				if(dataItemTemp.week != null){
					dataItemTemp.weekEntity = this.parseStringToEntity(dataItemTemp.week,5);
					dataItemTemp.week=this.parseStringToArray(dataItemTemp.week);
				}
				if(dataItemTemp.day != null){
					dataItemTemp.dayEntity = this.parseStringToEntity(dataItemTemp.day,6);
					if(dataItemTemp.day.indexOf("all")!=-1) dataItemTemp.dayEntity=dataItemTemp.dayEntity.concat([0]);
					dataItemTemp.day=this.parseStringToArray(dataItemTemp.day);
				}
				if(dataItemTemp.date != null){
					dataItemTemp.dateEntity = this.parseStringToEntity(dataItemTemp.date,31);
					dataItemTemp.date = this.parseStringToArray(dataItemTemp.date);
				}
				if(dataItemTemp.fullDate != null) dataItemTemp.fullDate=this.dateStringPaser.apply(this,this.parseStringToArray(dataItemTemp.fullDate));

				this.dataTemp = this.dataTemp.concat( [dataItemTemp] );
			}
			return this;
		}
};
var Calendarpicker=function(data){
	this.parseData(data);
};
$.extend(Calendarpicker.prototype,calendarPickerBaseMethods);
$.calendarpicker=function(data){return new Calendarpicker(data);}
$.calendarpicker.uuid = new Date().getTime();
$.calendarpicker.version = "0.0.1";
})(jQuery);